package com.sywb.chuangyebao.view.dialog;

import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sywb.chuangyebao.R;

import org.bining.footstone.utils.ScreenUtils;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.annotations.NonNull;

public class ConfirmAlertDialog extends BaseDialogFragment{
    @BindView(R.id.confirm_message_tv)
    TextView confirmMessageTv;
    @BindView(R.id.confirm_tv)
    TextView confirmTv;
    @BindView(R.id.dialog_ll)
    LinearLayout dialogLl;
    private String message;
    private String bottom;
    private AlertDialog.OnClickListener mClickListener;
    @Override
    public int getLayoutId(Bundle bundle) {
        return R.layout.layout_customize_pop;
    }

    @Override
    public void initView(View view, Bundle bundle) {
        if (bundle == null) {
            bundle = getArguments();
        }
        message = bundle.getString("message");
        bottom = bundle.getString("bottom");
        confirmMessageTv.setText(message);

        if(!TextUtils.isEmpty(bottom)) {
            confirmTv.setText(bottom);
        }
        getDialog().getWindow().setGravity(Gravity.CENTER);
        getDialog().setCancelable(false);
        getDialog().setCanceledOnTouchOutside(false);
        getDialog().setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    return true;
                }
                return false;
            }
        });
    }
    public static ConfirmAlertDialog newInstance( @NonNull String message,String bottom) {
        ConfirmAlertDialog dialogFragment = new ConfirmAlertDialog();
        Bundle bundle = new Bundle();
        bundle.putSerializable("message", message);
        bundle.putSerializable("bottom", bottom);
        dialogFragment.setArguments(bundle);
        return dialogFragment;
    }

    @OnClick(R.id.confirm_tv)
    @Override
    public void onClick(View v) {
            if (mClickListener != null) {
                mClickListener.onClick(0);
            }
            exit();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putString("message", message);
        outState.putString("bottom", bottom);
        super.onSaveInstanceState(outState);
    }

    public void setClickListener(AlertDialog.OnClickListener clickListener) {
        this.mClickListener = clickListener;
    }

    public interface OnClickListener {
        /**
         * 按钮点击时间处理
         * @param position 0左边按钮点击、1右边按钮点击
         */
        void onClick(int position);
    }
}
